<?php
namespace app\beautiful\controller\miniapp\member\project;
use app\BaseController;
use app\beautiful\logic\order\OrderLogic;
use app\beautiful\model\project\ProjectModel;
use app\beautiful\model\project\ProjectTypeModel;
use app\beautiful\model\project\SnapUpModel;
use app\beautiful\model\member\MemberSnapUpModel;
use think\facade\Db;

class Snapup extends BaseController{
    
    //我的次卡
    public function getMemberSnapupList(){
        $type = $this->request->param('type/d');
        $where = [
            ['shop_id','=',$this->request->shopId],
            ['member_id','=', $this->request->memberId]
        ];
        $today = date('Y-m-d',time());
        
        if($type == 0){
            $where[] = ['status','=',1];
            $where[] = ['valid_end_time','>=',$today];
            $query = MemberSnapUpModel::where($where);
        }else{
     
            $where1 = $where;
            $where2 = $where;
            $where1[] = ['status','=',8];
            $where2[] =  ['valid_end_time','<',$today];
            
            $query = MemberSnapUpModel::whereOr([$where1,$where2]);
        }
        $datas = $query->order(['member_snap_up_id'=>'desc'])->page($this->request->page)->limit($this->request->limit)->select()->append(['status_another'])->toArray();
        return $this->success(['datas'=>$datas]);
    }

    public function getMemberSnapupDetail(){
        $member_snap_up_id = $this->request->param('member_snap_up_id/d');
        if(empty($member_snap_up_id)){
            return $this->error('请选择要查看的抢购');
        }
        if(null === $snap_up = MemberSnapUpModel::find($member_snap_up_id)){
            return $this->error('请选择要查看的抢购');
        }
        if($snap_up->member_id != $this->request->memberId || $snap_up->shop_id != $this->request->shopId){
            return $this->error('请选择要查看的抢购');
        }
        $snap_up->append(['status_means','status_another','pay_time_format','pay_type_mean'])->toArray();
        return $this->success([
            'detail' => $snap_up
        ]);
    }
    
    public function getSnapupList()
    {

        $project_type_id=$this->request->param('project_type_id/d',0);

        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['status','=',1],
            
        ];
        if($project_type_id > 0){
            $where[] = ['project_type_id','=',$project_type_id];
        }

        $project_where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
        ];
        $project_ids= ProjectModel::where($project_where)->column('project_id') ?? [];
        $where[]=['project_id','in',$project_ids];

        $snap_up_model = new SnapUpModel();
        $snap_up_model=$snap_up_model->with(['project'=>function($query){
            $query->field('project_id,cover_img,abbreviation,market_price');
        }]);
        $order=['sort'=>'desc','add_time'=>'desc'];
        $field='snap_up_id,project_id,vip_price,is_flash_sale,flash_price,start_time,end_time,flash_num,fictitious_take_count';
        $append=['is_flash_sale_expire','flash_sale_djs'];
        $datas=$snap_up_model->where($where)->order($order)
            ->field($field)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->select()
            ->append($append)
            ->toArray();
        $return['datas']=$datas;
        return $this->success($return);

    }

    public function getProjectTypeList()
    {
        $datas=ProjectTypeModel::where([
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]
        ])->field('project_type_id,name')->select()->toArray();
        $return['datas']=$datas;
        return $this->success($return);
    }

    public function getSnapupDetail()
    {
        $snap_up_id=$this->request->param('snap_up_id/d',0);
        if($snap_up_id==0) return $this->error('抢购不存在');

        if(null === $snap_up = SnapUpModel::find($snap_up_id)) return $this->error('抢购不存在');
        if($snap_up->shop_id != $this->request->shopId) return $this->error('抢购不存在');
        if($snap_up->is_delete != 0) return $this->error('抢购不存在');

        if(null === $project = ProjectModel::find($snap_up->project_id))  return $this->error('抢购不存在');
        if($project->shop_id != $this->request->shopId) return $this->error('抢购不存在');
        if($project->is_delete != 0)return $this->error('抢购不存在');

        $detail=[
            'project'=>[
                'project_id'=>$project->project_id,
                'abbreviation'=>$project->abbreviation,
                'market_price'=>$project->market_price,
                'cover_img'=>$project->cover_img,
                'full_name' => $project->full_name,
            ],
            'vip_price'=>$snap_up->vip_price,
            'is_flash_sale'=>$snap_up->is_flash_sale,
            'flash_price'=>$snap_up->flash_price,
            'is_flash_sale_expire'=>$snap_up->is_flash_sale_expire,
            'flash_sale_djs'=>$snap_up->flash_sale_djs,
            'flash_num'     => $snap_up->flash_num,
            'valid_time'=>$snap_up->valid_time,
            'valid_unit'=>$snap_up->valid_unit,
            'fictitious_take_count'=>$snap_up->fictitious_take_count,
            'rotation'=>$snap_up->rotation,
            'snap_up_details'=>$snap_up->snap_up_details,
            'purchase_information'=>$snap_up->purchase_information,
            'sort'=>$snap_up->sort,
        ];

        return $this->success(['detail'=>$detail]);
    }

    public function createOrder()
    {
        $snap_up_id=$this->request->param('snap_up_id/d',0);
        $total_price = $this->request->param('total_price/f',0);
        $use_integral = $this->request->param('use_integral/d',0);
        $coupon_id = $this->request->param('coupon_id/d',0);
        $num=$this->request->param('num/d',0);
        $params=[
            'member_id'=>$this->request->memberId,
            'total_price'=>$total_price,
            'use_integral'=>$use_integral,
            'coupon_id'=>$coupon_id,
            'snap_up_id'=>$snap_up_id,
            'num'=>$num,
        ];
        Db::startTrans();
        try{
            $order_logic = OrderLogic::init($this->request->shopId);
            $order_id=$order_logic['snap_up']->createOrder($params);
            Db::commit();
            return $this->success(['order_id'=>$order_id]);
        } catch (\Exception $e){
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }

    public function moneyPay()
    {
        $snap_up_order_id=$this->request->param('snap_up_order_id/d',0);
        $params=[
            'member_id'=>$this->request->memberId,
            'snap_up_order_id'=>$snap_up_order_id,
        ];
        Db::startTrans();
        try{
            $order_logic = OrderLogic::init($this->request->shopId);
            $order_logic['snap_up']->moneyPay($params);
            Db::commit();
            return $this->success('余额支付成功');
        } catch (\Exception $e){
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }

    public function cancelOrder()
    {
        $snap_up_order_id=$this->request->param('snap_up_order_id/d',0);
        $params=[
            'member_id'=>$this->request->memberId,
            'snap_up_order_id'=>$snap_up_order_id,
        ];
        Db::startTrans();
        try{
            $order_logic = OrderLogic::init($this->request->shopId);
            $order_logic['snap_up']->cancelOrder($params);
            Db::commit();
            return $this->success('取消订单成功');
        } catch (\Exception $e){
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }


}
